// pages/sound/index.js
const app = getApp()
const record_manager = wx.getRecorderManager()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    recording:false,
    recode:false,
    authod:false,
    result:''
  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.get_recod()
  },
  get_recod(){
    let that = this
    wx.getSetting().then(res =>{
       if(res.authSetting['scope.address.record']){
        that.setData({
          authod:true
         })
       }else{
        wx.authorize({
          scope: 'scope.record', })
          .then(res=>{
            that.setData({
              authod:true
             })
        })
        .catch(err=>{
          that.get_recod_auth()
        })
       }
    })
  },
  get_recod_auth(){
    let that = this
    wx.showModal({
      title: '提示',
      content: '未授权无法进行录音功能',
      cancelText:'不授权',
      confirmText:'授权',
      success (res) {
        if (res.confirm) {
          wx.openSetting({
            success (res) {
             if(res.authSetting['scope.record']){
               that.setData({
                 authod:true
               })
             }
            }
          })
        }
      }
    })
  },
  star_recod(){
    console.log(111)
    // this.get_recod()
    const options = {
      sampleRate:8000,
      numberOfChannels:1,
      encodeBitRate:48000,
      format:'PCM'
    }
    record_manager.start(options)
      this.setData({
        recode:true,
        recording:true
      })
    
  },
  end_recod(){
    console.log(122)
    record_manager.stop()
    this.setData({
      recode:false,
      recording:false
    })
    this.bind_stop()
  },
  bind_stop(){
    var that = this
    record_manager.onStop(res=>{
      var tf = res.tempFilePath;// 录音缓存地址
      const fs = wx.getFileSystemManager()// 读取文件
      fs.readFile({
        filePath:tf,
        success(res){
          const buffer = res.data// 存储到buffer
          that.audio_rec(buffer)// 将录音文件传到云函数的接口
        }
      })
    })
  },
  audio_rec(data){
    console.log(222)
    let that = this
    wx.showLoading({
      title: '语音识别中',
    })
    wx.cloud.callFunction({
      name:'audio_rec',
      data:{data}
    }).then(res=>{
      console.log(res)
      if(res.errMsg == "cloud.callFunction:ok" && res.result.err_no==0){
        var result_list = res.result.result
        var result = (result_list.join('')).replace(/。/g,'')
        if(result_list == ''){
          wx.showToast({
            title: '对不起，我没有听清楚，请再说一遍',
            icon:'none'
          })
        }
        // wx.navigateTo({
        //   url: '../../pages/search/index',
        // })
        
        that.setData({
          result
        })
      }else{
        console.log(res.result.err_msg)
        wx.showToast({
          title: '语音识别失败1',
          icon:'none'
        })
      }
      wx.hideLoading()
    })
    .catch(err=>{
      console.log(err)
      wx.showToast({
        title: '语音识别失败',
        icon:'none'
      })
    })
  },
  jump(){
    wx.navigateTo({
          url: '../../pages/search/index',
        })
    app.globalData.res =this.data.result
    
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})